//
//  ViewController.m
//  EKRecurrenceRuleDemo
//
//  Created by Mac on 2016/12/16.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "ViewController.h"
#import "CalendarRepSelect.h"

@interface ViewController ()<CalendarRepSelectDelegate,UITableViewDelegate,UITableViewDataSource> {
    NSString *_curRule;//规则字符串
    int64_t _ruleBeginDate;//规则开始时间 2016-10-01 00:00:00
    int64_t _ruleEndDate;//规则结束时间 2016-12-31 23:59:59
    
    NSArray *_dataRuleArr;//触发时间数组
}
@property (weak, nonatomic) IBOutlet UILabel *ruleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化数据源
    _dataRuleArr = @[];
    //设置默认的规则为每一天
    _curRule = @"FREQ=DAILY;INTERVAL=1";
    //设置规则开始/结束时间
    NSDateFormatter * df2 = [[NSDateFormatter alloc] init];
    [df2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate * beginDate =[df2 dateFromString:[NSString stringWithFormat:@"2016-10-01 00:00:00"]];
    _ruleBeginDate = beginDate.timeIntervalSince1970;
    NSDate * endDate =[df2 dateFromString:[NSString stringWithFormat:@"2016-12-31 23:59:59"]];
    _ruleEndDate = endDate.timeIntervalSince1970;
    //配置表格视图
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //设置开始的值
    [self getTime];
    [_tableView reloadData];
    // Do any additional setup after loading the view, typically from a nib.
}
//规则选择
- (IBAction)selectPreClicked:(id)sender {
    CalendarRepSelect *select = [CalendarRepSelect new];
    select.delegate = self;
    select.userSelectEKRecurrenceRule = [[EKRecurrenceRule alloc] initWithString:_curRule];
    select.providesPresentationContextTransitionStyle = YES;
    select.definesPresentationContext = YES;
    select.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:select animated:NO completion:nil];
}
//计算出该规则下的事件触发时间
- (void)getTime {
    //算出第一个应该触发的时间 2016-10-01 08:30:00
    int64_t firstDateSecond = _ruleBeginDate + 8 * 60 * 60 + 30 * 60 + 0;
    Scheduler * s = [[Scheduler alloc] initWithDate:[NSDate dateWithTimeIntervalSince1970:firstDateSecond] andRule:_curRule];
    //得到所有的时间 传入的是
    //2016-10-01 00:00:00
    //2016-12-31 23:59:59
    NSMutableArray * occurences = [[s occurencesBetween:[NSDate dateWithTimeIntervalSince1970:_ruleBeginDate] andDate:[NSDate dateWithTimeIntervalSince1970:_ruleEndDate]] mutableCopy];
    //去掉超出计算范围的时间  可以进入occurencesBetween看看原因
    if(occurences.count) {
        NSDate *firstDate = occurences.firstObject;
        if(firstDate.timeIntervalSince1970 < firstDateSecond)
            [occurences removeObject:firstDate];
    }
    if(occurences.count) {
        NSDate *lastDate = occurences.lastObject;
        if(lastDate.timeIntervalSince1970 < firstDateSecond)
            [occurences removeObject:lastDate];
    }
    _dataRuleArr = [occurences copy];
}
#pragma mark --
#pragma mark -- UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataRuleArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    }
    cell.textLabel.text = [_dataRuleArr[indexPath.row] description];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark --
#pragma mark -- CalendarRepSelectDelegate
-(void)calendarRepSelect:(EKRecurrenceRule *)eKRecurrenceRule {
    _curRule = [eKRecurrenceRule rRuleString];
    _ruleLabel.text = [NSString stringWithFormat:@"当前重复规则：%@",[eKRecurrenceRule rRepeatString]];
    [self getTime];
    [_tableView reloadData];
}


@end
